/**
 * Copyright 2020 StreamSets Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.streamsets.datacollector.restapi;

import com.streamsets.datacollector.restapi.rbean.lang.RString;
import com.streamsets.datacollector.restapi.rbean.rest.OkPaginationRestResponse;
import com.streamsets.datacollector.restapi.rbean.rest.OkRestResponse;
import com.streamsets.datacollector.restapi.rbean.rest.PaginationInfo;
import com.streamsets.datacollector.security.usermgnt.UsersManager;
import com.streamsets.datacollector.util.AuthzRole;

import javax.annotation.security.RolesAllowed;
import javax.inject.Inject;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import java.io.IOException;
import java.util.List;
import java.util.stream.Collectors;

@Path("/v1/usermanagement/groups")
@RolesAllowed(AuthzRole.ADMIN)
@Produces(MediaType.APPLICATION_JSON)
public class GroupManagementResource {
  private final UsersManager usersManager;

  @Inject
  public GroupManagementResource(UsersManager usersManager) {
    this.usersManager = usersManager;
  }

  @GET
  public OkPaginationRestResponse<RString> list(@Context PaginationInfo paginationInfo) throws IOException {
    paginationInfo = (paginationInfo != null) ? paginationInfo : new PaginationInfo();
    List<RString> groups = usersManager.listGroups()
        .stream()
        .map(RString::new)
        .collect(Collectors.toList());
    return new OkPaginationRestResponse<RString>(paginationInfo).setHttpStatusCode(OkRestResponse.HTTP_OK)
        .setData(groups);
  }

}
